import org.dreamwork.text.TextParser;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 14-11-24
 * Time: 上午11:16
 */
public class SimpleParser extends TextParser {
    public SimpleParser (File file) throws IOException {
        super (file);
    }

    public void parse () throws IOException {
/*
        File file = new File ("d:/tmp/def.java");
        OutputStream out = new FileOutputStream (file);
        PrintWriter writer = new PrintWriter (out, true);
*/
        try {
            Segment s = new Segment (skipSpace (), 0);
            String text = "";
            boolean inJavaCode = false;
            for (int ch = nextChar (); ch != -1; ch = nextChar ()) {
                switch (ch) {
                    case '<' :
                        char peek = stream [cursor];
                        if (peek == '%') {
                            s.start += 2;
                            char next = stream [cursor + 1];
//                                 third = stream [cursor + 2];
                            if (next == '=') {
                                s.start ++;
                            }
                            s = skipUntil ("%>");
                            s.length -= 2;
                            if (next != '@') {
                                text = s.toString ().trim ();
                                if (next == '=') {
                                    System.out.println ("pw.println (" + text + ");");
                                } else
                                    System.out.println (text);
                            }
                        } else if (peek == '@') {
                            continue;
                        } else {
                            s.start = cursor - 1;
                        }
                        break;
                    case '>' :
                        char last = stream [cursor - 2];
                        if (last == '%' && inJavaCode) {
                            inJavaCode = false;
                            s.length -= 2;
                            System.out.println (s);
                        }
                        break;
                    case '\n':
                        s.length = cursor - s.start;
                        text = s.toString ().trim ();
                        if (text.equals ("<%")) {
                            s.start = cursor + 2;
                            inJavaCode = true;
                        } else {
                            text = text.replace ("\"", "\\\"");
                            System.out.println ("pw.println (\"" + text + "\");");
                            s.start = cursor + 1;
                            s.length = 0;
                        }
/*
                        String line = s.toString ().replace ("\"", "\\\"").trim ();


*/
                    default:
//                        writer.write (ch);
                }
            }
        } finally {
/*
            out.flush ();
            out.close ();
*/
        }
    }

    public static void main (String[] args) throws IOException {
        new SimpleParser (new File ("d:/tmp/abc.txt")).parse ();
/*
        BufferedReader reader = new BufferedReader (new FileReader (new File ("d:/tmp/abc.txt")));
        String line;
        while ((line = reader.readLine ()) != null) {
            System.out.println (line);
        }
*/
    }
}